home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Button / Sources / Actions.cpp next >
Encoding:
Text File  |  1995-11-08  |  13.0 KB  |  448 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Actions.h
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Jim Lloyd
  7. //
  8. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef ACTIONS_H
  13. #include "Actions.h"
  14. #endif
  15.  
  16. #ifndef BUTTONDEF_H
  17. #include "ButtonDef.h"
  18. #endif
  19.  
  20. // ----- OS Layer -----
  21.  
  22. #ifndef FWBARRAY_H
  23. #include "FWBArray.h"
  24. #endif
  25.  
  26. #ifndef FWMEMMGR_H
  27. #include "FWMemMgr.h"
  28. #endif
  29.  
  30. #ifndef FWMEMHLP_H
  31. #include "FWMemHlp.h"
  32. #endif
  33.  
  34. #ifndef FWMEMHLP_H
  35. #include "FWMemHlp.h"
  36. #endif
  37.  
  38. #ifndef FWFILESP_H
  39. #include "FWFileSp.h"
  40. #endif
  41.  
  42. #ifndef FWRESOUR_H
  43. #include "FWResour.h"
  44. #endif
  45.  
  46. // ----- OpenDoc Includes -----
  47.  
  48. #ifndef _ODTYPES_
  49. #include <ODTypes.h>
  50. #endif
  51.  
  52. #ifndef SOM_Module_OpenDoc_StdProps_defined
  53. #include <StdProps.xh>
  54. #endif
  55.  
  56. #ifndef SOM_ODStorageUnit_xh
  57. #include <StorageU.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODTranslation_xh
  61. #include <Translt.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODSession_xh
  65. #include <ODSessn.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODDragItemIterator_xh
  69. #include <DgItmIt.xh>
  70. #endif
  71.  
  72. // ----- Macintosh Includes -----
  73.  
  74. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  75. #include <Drag.h>
  76. #endif
  77.  
  78. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  79. #include <Sound.h>
  80. #endif
  81.  
  82. #ifndef __OSA__
  83. #include <OSA.h>
  84. #endif
  85.  
  86. //========================================================================================
  87. //    class CAction
  88. //========================================================================================
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    CAction::CAction
  92. //----------------------------------------------------------------------------------------
  93.  
  94. CAction::CAction()
  95. {
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------
  99. //    CAction::~CAction
  100. //----------------------------------------------------------------------------------------
  101.  
  102. CAction::~CAction()
  103. {
  104. }
  105.  
  106. //========================================================================================
  107. //    class CSoundAction
  108. //========================================================================================
  109.  
  110. //----------------------------------------------------------------------------------------
  111. //    CSoundAction::CSoundAction
  112. //----------------------------------------------------------------------------------------
  113.  
  114. CSoundAction::CSoundAction()
  115. {
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. //    CSoundAction::~CSoundAction
  120. //----------------------------------------------------------------------------------------
  121.  
  122. CSoundAction::~CSoundAction()
  123. {
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. //    CSoundAction::IsInStorage
  128. //----------------------------------------------------------------------------------------
  129.  
  130. FW_Boolean CSoundAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
  131. {
  132.     FW_Boolean result = false;
  133.     if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))        // 'snd ' on Scrap
  134.         result = true;
  135.     else if (storage->Exists(ev, kODPropContents, kSoundFileKind, 0))    // sound file
  136.         result = true;
  137.     return result;
  138. }
  139.  
  140. //----------------------------------------------------------------------------------------
  141. //    CSoundAction::Internalize
  142. //----------------------------------------------------------------------------------------
  143.  
  144. void CSoundAction::Internalize(Environment* ev, 
  145.                                 ODStorageUnit* storage)
  146. {
  147.     FW_Boolean internalized = FALSE;
  148.     
  149.     if (storage->Exists(ev, 
  150.                     kODPropContents, kSoundScrapKind, 0))
  151.     {
  152.         // Mac 'snd ' in Scrap
  153.         storage->Focus(ev, 
  154.                         kODPropContents, 
  155.                         kODPosUndefined, 
  156.                         kSoundScrapKind, 
  157.                         0,
  158.                         kODPosUndefined);
  159.         InternalizeSound(ev, storage);
  160.     }
  161.     else if (storage->Exists(ev, 
  162.                     kODPropContents, kSoundFileKind, 0))    
  163.     {
  164.         // Mac sound file
  165.         storage->Focus(ev, 
  166.                         kODPropContents, 
  167.                         kODPosUndefined, 
  168.                         FW_CPart::gMachfsDataType, 
  169.                         0, 
  170.                         kODPosUndefined);
  171.         InternalizeSoundFile(ev, storage);
  172.     }
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    CSoundAction::InternalizeSound
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void CSoundAction::InternalizeSound(Environment* ev, 
  180.                                 ODStorageUnit* storage)
  181. {
  182.     // Assume the storage unit is already focused
  183.     unsigned long size = storage->GetSize(ev);
  184.     if (size > 0)
  185.     {
  186.         FW_CAcquireTemporarySystemHandle handle(size);
  187.         FW_CByteArray byteArray;
  188.         storage->GetValue(ev, size, byteArray);
  189.         byteArray.CopyBuffer(handle.GetPointer(), size);
  190.         fSoundHandle = handle.GetPlatformHandle();
  191.         handle.Orphan();
  192.     }
  193. }
  194.  
  195. //----------------------------------------------------------------------------------------
  196. //    CSoundAction::InternalizeSoundFile
  197. //----------------------------------------------------------------------------------------
  198.  
  199. void CSoundAction::InternalizeSoundFile(Environment* ev, 
  200.                                     ODStorageUnit* storage)
  201. {
  202.     // StorageUnit should already be Focused on HFSFlavor
  203.     unsigned long hfsSize = storage->GetSize(ev);
  204.     if (hfsSize > 0)
  205.     {
  206.         // Get the HFS flavor
  207.         HFSFlavor hfsInfo;
  208.         FW_CByteArray byteArray;
  209.         storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
  210.         byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
  211.         
  212.         // Create platform independent file spec
  213.         FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
  214.         
  215.         // Open the resource file
  216.         FW_CResourceFile resourceFile(fileSpec);
  217.         
  218.         // Get any (i.e the first) sound resource
  219.         const FW_ResourceType resourceType = 'snd ';
  220.         const FW_ResourceId resourceId = 1;
  221.         Handle sound = ::Get1IndResource(resourceType, 
  222.                                         resourceId);
  223.                                         
  224.         if (sound != NULL)
  225.         {
  226.             // if succeeded, detach the handle and keep it
  227.             ::DetachResource(sound);
  228.             fSoundHandle = sound;
  229.         }
  230.         else
  231.         {
  232.             // No sound resource loaded, throw an exception
  233.             FW_FailOnError(::ResError());
  234.             FW_Failure(resNotFound);
  235.         }
  236.     }
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. //    CSoundAction::Externalize
  241. //----------------------------------------------------------------------------------------
  242.  
  243. void CSoundAction::Externalize(Environment* ev, 
  244.                                 ODStorageUnit* storage)
  245. {
  246.     // Assume we're focused on the correct property
  247.     if (!storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
  248.         storage->AddValue(ev, kSoundScrapKind);
  249.     
  250.     if (fSoundHandle != NULL)
  251.     {
  252.         FW_CAcquireLockedSystemHandle lock(fSoundHandle);
  253.         FW_CByteArray byteArray(*fSoundHandle, 
  254.                     FW_CMemoryManager::
  255.                     GetSystemHandleSize(fSoundHandle));
  256.         storage->SetValue(ev, byteArray);
  257.     }
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. // CSoundAction::DoIt
  262. //----------------------------------------------------------------------------------------
  263.  
  264. void CSoundAction::DoIt()
  265. {
  266.     if (fSoundHandle != NULL)
  267.     {
  268.         FW_CAcquireLockedSystemHandle lock(fSoundHandle);
  269.         ::SndPlay(NULL, (SndListHandle)fSoundHandle, TRUE);
  270.     }
  271. }
  272.  
  273. //========================================================================================
  274. //    class CScriptAction
  275. //========================================================================================
  276.  
  277. //----------------------------------------------------------------------------------------
  278. //    CScriptAction::CScriptAction
  279. //----------------------------------------------------------------------------------------
  280.  
  281. CScriptAction::CScriptAction()
  282. {
  283. }
  284.  
  285. //----------------------------------------------------------------------------------------
  286. //    CScriptAction::~CScriptAction
  287. //----------------------------------------------------------------------------------------
  288.  
  289. CScriptAction::~CScriptAction()
  290. {
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    CScriptAction::IsInStorage
  295. //----------------------------------------------------------------------------------------
  296.  
  297. FW_Boolean CScriptAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
  298. {
  299.     FW_Boolean result = false;
  300.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))        // 'scpt' on Scrap
  301.         result = true;
  302.     else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0))    // compiled script file
  303.         result = true;
  304.     return result;
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. //    CScriptAction::Internalize
  309. //----------------------------------------------------------------------------------------
  310.  
  311. void CScriptAction::Internalize(Environment* ev, ODStorageUnit* storage)
  312. {
  313.     FW_Boolean internalized = FALSE;
  314.     
  315.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))    // 'scpt' on Scrap
  316.     {
  317.         storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
  318.         InternalizeScript(ev, storage);
  319.     }
  320.     else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0))    // compiled script file
  321.     {
  322.         storage->Focus(ev, kODPropContents, kODPosUndefined, FW_CPart::gMachfsDataType, 0, kODPosUndefined);
  323.         InternalizeScriptFile(ev, storage);
  324.     }
  325. }
  326.  
  327. //----------------------------------------------------------------------------------------
  328. //    CScriptAction::InternalizeScript
  329. //----------------------------------------------------------------------------------------
  330.  
  331. void CScriptAction::InternalizeScript(Environment* ev, ODStorageUnit* storage)
  332. {
  333.     // Assume the storage unit is already focused
  334.     unsigned long size = storage->GetSize(ev);
  335.     if (size > 0)
  336.     {
  337.         FW_CAcquireTemporarySystemHandle handle(size);
  338.         FW_CByteArray byteArray;
  339.         storage->GetValue(ev, size, byteArray);
  340.         byteArray.CopyBuffer(handle.GetPointer(), size);
  341.         fScriptHandle = handle.GetPlatformHandle();
  342.         handle.Orphan();
  343.     }
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. //    CScriptAction::InternalizeScriptFile
  348. //----------------------------------------------------------------------------------------
  349.  
  350. void CScriptAction::InternalizeScriptFile(Environment* ev, 
  351.                                     ODStorageUnit* storage)
  352. {
  353.     // StorageUnit should already be Focused on HFSFlavor
  354.     unsigned long hfsSize = storage->GetSize(ev);
  355.     if (hfsSize > 0)
  356.     {
  357.         // Get the HFS flavor
  358.         HFSFlavor hfsInfo;
  359.         FW_CByteArray byteArray;
  360.         storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
  361.         byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
  362.         
  363.         // Make the file specification
  364.         FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
  365.         
  366.         // Acquire an opened resource file, 
  367.         // will be closed automatically
  368.         FW_CResourceFile resourceFile(fileSpec);
  369.         
  370.         // Acquire an opened resource, 
  371.         // will be released automatically
  372.         FW_CResource resource(resourceFile, 128, 'scpt');
  373.         FW_CAcquireResourceData lockedResource(resource);
  374.         
  375.         // Copy the resource data into a new handle
  376.         FW_CAcquireTemporarySystemHandle lockedHandle(lockedResource.GetSize());
  377.         FW_CMemoryManager::CopyMemory(lockedResource.GetData(), lockedHandle.GetPointer(), lockedResource.GetSize());
  378.         fScriptHandle = lockedHandle.GetPlatformHandle();
  379.         lockedHandle.Orphan();
  380.     }
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. //    CScriptAction::Externalize
  385. //----------------------------------------------------------------------------------------
  386.  
  387. void CScriptAction::Externalize(Environment* ev, ODStorageUnit* storage)
  388. {
  389.     if (!storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
  390.         storage->AddValue(ev, kScriptScrapKind);
  391.     
  392.     if (fScriptHandle != NULL)
  393.     {
  394.         // Acquire a lock on the system handle, will be unlocked automatically
  395.         FW_CAcquireLockedSystemHandle lock(fScriptHandle);
  396.         
  397.         // Copy the data into a SOM compatible object, and then write it to the storage unit
  398.         FW_CByteArray byteArray(lock.GetPointer(), FW_CMemoryManager::GetSystemHandleSize(fScriptHandle));
  399.         storage->SetValue(ev, byteArray);
  400.     }
  401. }
  402.  
  403. //----------------------------------------------------------------------------------------
  404. //    LoadAndExecuteScript
  405. //----------------------------------------------------------------------------------------
  406. // This function was lifted almost verbatim from
  407. // IM:IAC, page 10-16. (Actually, I used MPTA, but it says 
  408. // the code is on the above page)
  409.  
  410. void CScriptAction::LoadAndExecuteScript(Handle scriptData)
  411. {
  412.     AEDesc scriptDesc;
  413.     OSAID scriptID, resultID;
  414.     OSAError error;
  415.     static ComponentInstance gComponent = 0;
  416.  
  417.     if (!gComponent)
  418.         gComponent = ::OpenDefaultComponent(
  419.                     kOSAComponentType, 
  420.                     kOSAGenericScriptingComponentSubtype);
  421.  
  422.     // load the script data
  423.     scriptDesc.descriptorType = typeOSAGenericStorage;
  424.     scriptDesc.dataHandle = scriptData;
  425.     error = ::OSALoad(gComponent, &scriptDesc, 
  426.                             kOSAModeNull, &scriptID);
  427.     
  428.     if (error == noErr)
  429.     { 
  430.         // execute the compiled script in the default context                 
  431.         error = ::OSAExecute(gComponent, 
  432.                         scriptID, kOSANullScript, 
  433.                         kOSAModeNull, &resultID);
  434.         error = ::OSADispose(gComponent, scriptID);
  435.         error = ::OSADispose(gComponent, resultID);
  436.     }
  437. }
  438.  
  439. //----------------------------------------------------------------------------------------
  440. // CScriptAction::DoIt
  441. //----------------------------------------------------------------------------------------
  442.  
  443. void CScriptAction::DoIt()
  444. {
  445.     if (fScriptHandle != NULL)
  446.         LoadAndExecuteScript(fScriptHandle);
  447. }
  448.